package chpt04;
public final class ImmutablePoint{
	private static int no = 0;  // 用来存储点序号
	private final int id;
	private final String name;
    private final double x;
    private final double y;
    {
    	id = ++no; // 初始化id
    }
	public ImmutablePoint(String name, double x, double y) {
		this.name = name;
		this.x = x;
		this.y = y;
	}
	// 获取坐标与当前点坐标相差xd、yd的点
	public ImmutablePoint getRelativePoint(String name, 
			double xd, double yd) {
		// 返回的是一个新的点对象，而未改变原来点的坐标
		return new ImmutablePoint(name, x + xd, y + yd);
	}
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public double getX() {
		return x;
	}
	public double getY() {
		return y;
	}
	
	@Override
	public String toString() {
		return "ImmutablePoint [id=" + id + ", name=" 
				+ name + ", x=" + x + ", y=" + y + "]";
	}
	
	public static void main(String[] args) {
		ImmutablePoint p1 = new ImmutablePoint("1st", 1.0, 2.0);
		ImmutablePoint p2 = p1.getRelativePoint("2nd", 2.0, 3.0);
		System.out.println(p1);
		System.out.println(p2);
	}
}
